
bt.module({builder: function(bt) {
    var j$ = jQuery;

    var _ellipsisPopup = function(item) {
        var ellipsis_opts = {max_rows: 1};
        var obj = j$(item);
        if(obj.data("ellipsised") != "true") {
            // destinations popup
            obj.find(".level-2 a").each(function() {
                j$(this).ThreeDots(ellipsis_opts);
            });
            obj.data("ellipsised", "true");
        }
    };

    var _ellipsisHotDestnations = function() {
        var ellipsis_opts = {max_rows: 1};
        // hot_destinations name
        j$(".hot-destinations .photos .photo .mask .name").each(function() {
            if(!j$(this).attr("threedots")) {
                j$(this).ThreeDots(ellipsis_opts);
            }
        });
    };

    var _ellipsis = function() {
        var ellipsis_opts = {max_rows: 1};
        // hot_destinations name
        _ellipsisHotDestnations();
        // hot_cities & hot_countries
        j$(".hot-panel .name a").each(function() {
            j$(this).ThreeDots(ellipsis_opts);
        });

    };

    var _initAutoLoadMoreEvent = function() {
        var body = j$("body.new-header");
        j$(window).scroll(function() {
            if (j$(".hot-destinations").attr("data-load-flag") == "1") {
                return;
            }
            if((j$(window).scrollTop() + j$(window).height()) + 10 >= body.height()) {
                j$(".hot-destinations").attr("data-load-flag", "1");
                var load_more = j$("#load-more");
                if (load_more.data("start") === undefined) return;
                j$("#loading-hint").show();
                j$('#ajax-loading').show();
                j$('#ajax-loading').css("visibility", "visible");
                j$.ajax({
                    url: "/hot_destinations/",
                    data: "start=" + load_more.data("start") + "&count=" + load_more.data("count"),
                    success: function(resp) {
                        if(resp) {
                            j$(".hot-destinations .photos").append(resp);
                        } else {
                            j$("#load-more").slideUp();
                        }
                    }
                }).done(function() {
                        _ellipsisHotDestnations();
                        load_more.remove();
                        j$("#loading-hint").remove();
                        j$(".hot-destinations").attr("data-load-flag", "0");
                    });
            }
        });
    };


    j$(document).ready(function() {
        j$(".scrollable").scrollable({circular: true, mousewheel: false}).navigator(".navi")
                .autoscroll({interval: 5000});
        j$(".featured .navi a").mouseover(function() {
            j$(this).click();
        });

        j$(".regions .domestic").fancybox({
            autoSize: true,
            closeBtn: false,
            padding: 0,
            maxWidth: 640,
            maxHeight: 360,
            helpers: {
                overlay: {
                    opacity: 0.3
                }
            }
        });
        j$(".regions .foreign").fancybox({
            autoSize: true,
            closeBtn: false,
            padding: 0,
            maxWidth: 640,
            maxHeight: 360,
            helpers: {
                overlay: {
                    opacity: 0.3
                }
            }
        });

        j$("#domestic-dest-popup .close, #foreign-dest-popup .close").click(function() {
            j$.fancybox.close();
        });

        _initAutoLoadMoreEvent();
        _ellipsis();
    });
}});
